<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  var arr = function (head) {
    const stack = [], res = [];
    let cur = head, i = 0;
    while (cur) {
      const val = cur.val;
      res[i] = 0;
      while (stack.length && stack[stack.length - 1].val < val) {
        let i = stack.pop().i;
        res[i] = val;
      }
      stack.push({ i, val });
      i++;
      cur = cur.next;
    }
    return res
  };
  console.log(arr(head = [2, 1, 5]));
</script>

</html>